package com.canbot.u05.utils;

import android.content.Context;
import android.os.AsyncTask;

import com.canbot.u05.activity.attendance.AttendanceShowInfoActivity;
import com.canbot.u05.bean.AttendaceExcelBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


/**
 *
 */

public class ExcelDataLoader extends AsyncTask<String, Void, ArrayList<AttendaceExcelBean>> {

        private Context context;

        public ExcelDataLoader(Context context) {
                this.context = context;
        }


        @Override
        protected ArrayList<AttendaceExcelBean> doInBackground(String... params) {
                return getXlsData(params[0], 0);
        }

        @Override
        protected void onPostExecute(ArrayList<AttendaceExcelBean> attendaceExcelBeen) {

                if (attendaceExcelBeen != null) {
//                        AttendanceShowInfoActivity a = (AttendanceShowInfoActivity) context;
                        AttendanceShowInfoActivity a = (AttendanceShowInfoActivity) context;
                        a.updateData(attendaceExcelBeen);

                }
        }

        /**
         * 获取 excel 表格中的数据,不能在主线程中调用
         */
        private ArrayList<AttendaceExcelBean> getXlsData(String xlsName, int index) {

                File file = new File(xlsName);
                if (!file.exists()) {
                        Logger.e("excel不存在");
                        return null;
                }

                ArrayList<AttendaceExcelBean> beanList = new ArrayList();

                Workbook workbook = null;
                try {
                        workbook = Workbook.getWorkbook(new FileInputStream(file));
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                catch (BiffException e) {
                        e.printStackTrace();
                }
                if (workbook == null) {
                        return null;
                }
                Sheet sheet = workbook.getSheet(index);

                int sheetNum = workbook.getNumberOfSheets();
                int sheetRows = sheet.getRows();
                int sheetColumns = sheet.getColumns();

//            Log.d(TAG, "the num of sheets is " + sheetNum);
//            Log.d(TAG, "the name of sheet is  " + sheet.getName());
//            Log.d(TAG, "total rows is 行=" + sheetRows);
//            Log.d(TAG, "total cols is 列=" + sheetColumns);

                // "姓名", "日期", "对应时间段", "上班时间","下班时间","签到时间","签退时间","应到","实到","迟到时间","早退时间","是否旷工","加班时间",""
                for (int i = 1; i < sheetRows; i++) {
                        AttendaceExcelBean bean = new AttendaceExcelBean();

                        bean.setName(sheet.getCell(0, i).getContents());
                        bean.setDate(sheet.getCell(1, i).getContents());
                        bean.setCorrespondingTimePeriod(sheet.getCell(2, i).getContents());
                        bean.setToWorkTime(sheet.getCell(3, i).getContents());
                        bean.setOffWorkTime(sheet.getCell(4, i).getContents());
                        bean.setCheckInTime(sheet.getCell(5, i).getContents());
                        bean.setCheckoutTime(sheet.getCell(6, i).getContents());
                        bean.setShouldArriveTime(sheet.getCell(7, i).getContents());
                        bean.setReallyTime(sheet.getCell(8, i).getContents());
                        bean.setLateTime(sheet.getCell(9, i).getContents());
                        bean.setLeaveEarly(sheet.getCell(10, i).getContents());
                        bean.setIsAbsenteeism(sheet.getCell(11, i).getContents());
                        bean.setOverTime(sheet.getCell(12, i).getContents());

                        beanList.add(bean);

                        Logger.e("AttendanceShowInfoActivity",bean.toString());
                }

                workbook.close();

                return beanList;
        }
}
